const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ['优惠券1', '优惠券2', '优惠券3'],
    index: "",
    ptype: 0,
    activeNum: 1,
    value1: 1,
    productId: 0,
    product: [],
    orderType: 1,
    message: '',
    total: '',
    address: {},
    postId: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.pid) {
      this.setData({
        productId: options.pid
      });
    }

    if (options.postId) {
      this.setData({
        postId: options.postId
      });
    }

    if (options.type) {
      this.setData({
        ptype: options.type
      });
    }

    this.getProductData();

    if (this.data.postId == 0) {
      this.getDefaultPost();
    } else {
      this.getPost();
    }
  },

  getProductData: function() {
    let pid = this.data.productId;
    let that = this;
    util.request(api.GetProductUrl, {
      id: pid
    }).then(function(res) {
      if (res.success) {
        let buyNum = util.getBuyNum(res.data.sellPrice);
        console.info('----'+buyNum)
        that.setData({
          product: res.data,
          total: res.data.sellPrice * buyNum,
          activeNum: buyNum,
          value1: buyNum
        });
      }
    });
  },
  handleChange1({
    detail
  }) {
    this.setData({
      value1: detail.value,
      total: detail.value * this.data.product.sellPrice
    })
  },
  bindMessage(event) {
    this.setData({
      message: event.detail.value
    });
  },
  goAddress: function() {
    let pid = this.data.productId;
    wx.navigateTo({
      url: '/pages/vip/addAddress/addAddress?pid=' + pid
    })
  },
  selectAddress: function() {
    wx.navigateTo({
      url: '/pages/vip/address/address?pid=' + this.data.productId
    })
  },
  getDefaultPost: function() {
    let that = this;
    util.request(api.DefaultPostUrl).then(function(res) {
      if (res.success) {
        that.setData({
          address: res.data
        });
      }
    });
  },
  getPost: function() {
    let that = this;
    util.request(api.GetPostUrl, {
      id: that.data.postId
    }).then(function(res) {
      if (res.success) {
        that.setData({
          address: res.data
        });
      }
    });
  },
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },

  toOrder: function() {
    let that = this;
    let totalMoney = that.data.total;
    let buyNum = that.data.value1;
    let productId = that.data.productId;
    let post = that.data.address;
    let orderType = that.data.orderType;
    let message = that.data.message;
    if (!post) {
      util.showErrorToast('请选择收货地址');
      return false;
    }
    util.requestPost(api.ToOrderUrl, {
      totalMoney: totalMoney,
      buyNum: buyNum,
      productId: productId,
      postId: post.id,
      orderType: orderType,
      message: message
    }).then(function(res) {
      if (res.success) {
        util.payOrder(res.data.id, 1, res.data.totalMoney).then(function(res) {
          if (res.success) {
            wx.showToast({
              title: res.message
            })
            //跳转要注意跳转页面类型
            wx.switchTab({
              url: "/pages/home/home",
            })
          } else {
            wx.showToast({
              title: res.errMsg
            })
            //跳转要注意跳转页面类型
            wx.switchTab({
              url: "/pages/home/home",
            })
          }
        });

      } else {
        util.showErrorToast(res.message);
      }
    });
  }
})